//
//  HomeViewController.m
//  KuWoCan
//
//  Created by hwd on 17/3/2.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "HomeViewController.h"
#import "AFNetworking.h"
// VC
#import "OrderManageVC.h"               //订单管理
#import "SaleApplyListVC.h"             //费用申请三合一
#import "NWNewsRequest.h"               //新闻接口
#import "NewsListModel.h"               //新闻Model
#import "NewsDetailViewController.h"    //新闻VC

//Cells
#import "HomePageADTableViewCell.h"     //广告Cell
#import "HomepageActionTableViewCell.h" //功能Cell


//CellIdentifier
static NSString *const HomepageAdCellIdentifier = @"HomeADCell";
static NSString *const HomepageActionCellIdentifier = @"HomeActionCell";

@interface HomeViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *table;
/** 图片数组 */
@property (nonatomic,strong) NSMutableArray *imgArr;
/** 广告内容 */
@property (nonatomic,strong) NSMutableArray *contentArr;
/** AD title */
@property (nonatomic, strong) NSMutableArray *titleArr;
@property (nonatomic,copy) NSArray *dataArr; // 数据源

@end

@implementation HomeViewController
#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    [self configRefreshControl];
    [self requestGetBanner];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    AppDelegate *applicationDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [applicationDelegate.tabBarController hideTabBar:NO];
   
    
}
-(void)viewDidAppear:(BOOL)animated{
   [ToolViewViewController addGengXin];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    AppDelegate *applicationDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [applicationDelegate.tabBarController hideTabBar:YES];
}

#pragma mark - Config UI
- (void)configUI {
    [self createCenterImgViewWithImg:kImg(@"LOGO")];
    [self createRightButtonWithImage:[UIImage imageNamed:@"customer"] selector:@selector(CallPhone)];
    self.view.backgroundColor = [UIColor colorWithHexString:@"fefdfd"];
//    [self createRightButton:@"切换" selector:@selector(changeIp)];

    [self.view addSubview:self.table];
}

- (void)configRefreshControl {
    self.table.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestGetBanner];
    }];
}

#pragma mark - Data request
- (void)requestGetBanner
{
    NWNewsRequest *request = [[NWNewsRequest alloc] init];
    [request completion:^(id result, NSString *message, id extension, BOOL succ) {
        if ([self.table.mj_header isRefreshing]) {
            [self.table.mj_header endRefreshing];
        }
        if (succ)
        {
            [self.imgArr removeAllObjects];
            [self.contentArr removeAllObjects];
            self.dataArr = [NewsListModel arrayOfModelsFromDictionaries:result];
            for (NewsListModel *model in self.dataArr)
            {
                [self.imgArr addObject:model.imgUrl];
                [self.contentArr addObject:[NSString stringWithFormat:@"%@\n%@",model.title,model.describes]];
            }
            [self.table reloadData];
        } else {
            [SVProgressHUD showErrorWithStatus:message];
        }
    }];
    [request startRequestGetBanner];
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 250 ;
    } else {
        return (SCREENW - 25 * screenW_6) / 23 * screenW_6 * 8 * 2 + 40 * screenW_6;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 18 * screenW_6;
}

// 返回控制颜色的头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor colorWithHexString:@"fefdfd"];
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    __weak typeof(self) weakSelf = self;
    if (indexPath.row == 0)
    {
        HomePageADTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:HomepageAdCellIdentifier];
        if (!cell) {
            cell = [[HomePageADTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:HomepageAdCellIdentifier];
        }
        [cell setAdCellTitlesGroup:self.contentArr imageURLStringsGroup:self.imgArr];

        cell.adCellClicked = ^(NSInteger index) {
            NewsListModel *model = weakSelf.dataArr[index];
            NewsDetailViewController *vc = [[NewsDetailViewController alloc] init];
            vc.newsId = model.id;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
        return cell;
    }
    else
    {
        HomepageActionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:HomepageActionCellIdentifier];
        if (!cell) {
            cell = [[HomepageActionTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:HomepageActionCellIdentifier];
        }
        [cell congfigActionCellWithPlist:@"HonePageConfiguration"];
        cell.actionViewCliecked = ^(ApplyType actionType) {
            [weakSelf jumpToApplyPage:actionType];
        };
        return cell;
    }
}

#pragma mark - private method
- (void)jumpToApplyPage:(ApplyType)actionType {
    switch (actionType) {
        case ApplyTypeOrderManage: {
            OrderManageVC *orderVC = [[OrderManageVC alloc] init];
            [self.navigationController pushViewController:orderVC animated:YES];
        }
            break;

        case ApplyTypeEntryFeeApply: {
            SaleApplyListVC *vc = [[SaleApplyListVC alloc] init];
            vc.ApplyTypeNum = ApplyTypeEntryFeeApply;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;

        case ApplyTypePromotionApply: {
            SaleApplyListVC *vc = [[SaleApplyListVC alloc] init];
            vc.ApplyTypeNum = ApplyTypePromotionApply;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;

        case ApplyTypeProductDisplayFeeApply: {
            SaleApplyListVC *vc = [[SaleApplyListVC alloc] init];
            vc.ApplyTypeNum = ApplyTypeProductDisplayFeeApply;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;

        case ApplyTypeOtherFeeApply: {
            SaleApplyListVC *vc = [[SaleApplyListVC alloc] init];
            vc.ApplyTypeNum = ApplyTypeOtherFeeApply;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        default:
            break;
    }
}

- (void)changeIp {
    BOOL netEnvironment = [[NSUserDefaults standardUserDefaults] boolForKey:@"netEnvironment"];
    if (!netEnvironment) {
        KwApiURL = @"http://192.168.0.250:8080";
        [NSUserDefaults saveBool:YES forkey:@"netEnvironment"];
        [SVProgressHUD showInfoWithStatus:@"切换为内网"];
    } else {
        KwApiURL = @"http://119.23.79.117:8080";
        [NSUserDefaults saveBool:NO forkey:@"netEnvironment"];
        [SVProgressHUD showInfoWithStatus:@"切换为外网"];
    }
}

#pragma mark - getter and setter
- (UITableView *)table {
    if (!_table) {
        _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENW, SCREENH - 64 - 60) style:UITableViewStylePlain];
        _table.delegate = self;
        _table.dataSource = self;
        _table.backgroundColor = [UIColor colorWithHexString:@"fefdfd"];
        _table.showsVerticalScrollIndicator = NO;
        _table.showsHorizontalScrollIndicator = NO;
    }
    return _table;
}

- (NSArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSArray array];
    }
    return _dataArr;
}

- (NSMutableArray *)imgArr {
    if (!_imgArr) {
        _imgArr = [NSMutableArray array];
    }
    return _imgArr;
}

- (NSMutableArray *)contentArr {
    if (!_contentArr) {
        _contentArr = [NSMutableArray array];
    }
    return _contentArr;
}

-(void)CallPhone//拨打客服
{
    [Constants callTelephoneWithPhone:@"4000716186" atViewController:self];
}
@end
